# -*- coding: utf-8 -*-


# =================== uvicorn日志配置 =====================
# import uvicorn
# log_config = uvicorn.config.LOGGING_CONFIG
# # http 请求时日志输出
# log_config["formatters"]["access"]["fmt"] = '%(asctime)s - %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
# # 默认的日志输出
# log_config["formatters"]["default"]["fmt"] = "%(asctime)s - %(levelname)s - %(message)s"
# 用于打印自定义日志信息


# =================== 自定义日志配置 =====================
import logging
import os


# 确保日志目录存在
os.makedirs("logs", exist_ok=True)
log_file = "logs/app.log"

# 检查并删除已存在的日志文件
if os.path.exists(log_file):
    try:
        os.remove(log_file)
    except Exception as e:
        _ = e


log_config = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "default": {
            "()": "uvicorn.logging.DefaultFormatter",
            "fmt": "%(asctime)s - %(levelprefix)s %(message)s",
            "use_colors": True,
        },
        "access": {
            "()": "uvicorn.logging.AccessFormatter",
            "fmt": '%(asctime)s - %(client_addr)s - %(levelprefix)s "%(request_line)s" %(status_code)s',  # noqa: E501
            "use_colors": True,
        },
        "log_default": {
                    "()": "uvicorn.logging.DefaultFormatter",
                    "fmt": "%(asctime)s - %(levelprefix)s %(message)s",
                    "use_colors": False,
                },
        "log_access": {
            "()": "uvicorn.logging.AccessFormatter",
            "fmt": '%(asctime)s - %(client_addr)s - %(levelprefix)s "%(request_line)s" %(status_code)s',  # noqa: E501
            "use_colors": False,
        },
    },
    "handlers": {
        "default": {
            "formatter": "default",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stderr",
        },
        "access": {
            "formatter": "access",
            "class": "logging.StreamHandler",
            "stream": "ext://sys.stdout",
        },
        "default_log": {
            "formatter": "log_default",
            "class": "logging.handlers.RotatingFileHandler",
            "filename": f"{log_file}",
            "maxBytes": 1024 * 1024 * 5,
            "backupCount": 5,
            "encoding": "utf-8",
        },
        "access_log": {
            "formatter": "log_access",
            "class": "logging.handlers.RotatingFileHandler",
            "filename": f"{log_file}",
            "maxBytes": 1024 * 1024 * 5,
            "backupCount": 5,
            "encoding": "utf-8",
        },
    },
    "loggers": {
        "uvicorn": {"handlers": ["default", "default_log"], "level": "INFO", "propagate": False},
        "uvicorn.error": {"level": "INFO"},
        "uvicorn.access": {"handlers": ["access", "access_log"], "level": "INFO", "propagate": False},
    },
}

logging.getLogger('passlib').setLevel(logging.ERROR)  # 使用日志记录配置将passlib警告静音：https://github.com/pyca/bcrypt/issues/684
logger = logging.getLogger("uvicorn")